package com.atguigu.yygh.user.controller;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.user.util.TokenUtils;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-03-14
 */
@RestController
@RequestMapping("/api/user/")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;


    //会员登录
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        // 调用方法继续调用
        Map<String, Object> map = userInfoService.login(loginVo);
        // 返回给页面对应的数据
        // 包括用姓名或者手机号
        return R.ok().data(map);
    }

    //用户认证接口
    @PostMapping("auth/userAuth")
    public R userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        Long userId = TokenUtils.getUserId(request);
        userInfoService.userAuth(userAuthVo, userId);
        return R.ok();
    }

    //用户认证接口
    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {
        Long userId = TokenUtils.getUserId(request);
        UserInfo userInfo = userInfoService.getUserInfo(userId);
        return R.ok().data("userInfo",userInfo);
    }
}

